package com.heima.wemedia.builder;

import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import org.springframework.beans.BeanUtils;

import java.util.Date;

public class ArticleDtoBuilder {
    private ArticleDto dto = new ArticleDto();
    private WmNews wmNews;
    private WmUserMapper wmUserMapper;
    private WmChannelMapper wmChannelMapper;
    //构造方法
    public ArticleDtoBuilder(WmNews wmNews, WmUserMapper wmUserMapper, WmChannelMapper wmChannelMapper) {
        this.wmNews = wmNews;
        this.wmUserMapper = wmUserMapper;
        this.wmChannelMapper = wmChannelMapper;
    }
    //属性拷贝
    public ArticleDtoBuilder buildBeanCopy(){
        BeanUtils.copyProperties(wmNews,dto);
        return this;
    }
    //作者
    public ArticleDtoBuilder buildAuthor(){
        WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
        if(wmUser != null){
            dto.setAuthorId(wmUser.getId().longValue());
            dto.setAuthorName(wmUser.getName());
        }
        return this;
    }
    //频道
    public ArticleDtoBuilder buildChannel(){
        WmChannel wmChannel = wmChannelMapper.selectById(wmNews.getChannelId());
        if(wmChannel != null){
            dto.setChannelName(wmChannel.getName());
        }
        return this;
    }
    //其他
    public ArticleDtoBuilder buildBasic(){
        dto.setLayout(wmNews.getType());
        dto.setCreatedTime(new Date());
        dto.setCollection(0);
        dto.setComment(0);
        dto.setViews(0);
        dto.setLikes(0);
        if(wmNews.getArticleId() != null){
            dto.setId(wmNews.getArticleId());
        }

        return this;
    }
    //返回
    public ArticleDto builder(){
        return dto;
    }

}
